package com.kongshidian.ui;

import java.lang.ref.WeakReference;
import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kongshidian.App;
import com.kongshidian.R;
import com.kongshidian.bean.CouponCode;
import com.kongshidian.http.WebClient;
import com.kongshidian.http.WebException;
import com.kongshidian.ui.view.AbsViewHolderAdapter;
import com.kongshidian.util.DateTimeUtil;

/**
 * @author fbl 
 *  选择优惠劵
 */
public class ChoiceCouponActivity extends BackableTitleBarActivity {
	@ViewInject(id = R.id.choice_coupon_empty_relat ) RelativeLayout mEmptyRelat;
	@ViewInject(id = R.id.choise_coupon_list ,itemClick = "onItemClick") ListView mChoiceCouponList;

	ChoiceCouponAdapter mAdapter;
	//	private int choiceId = 0;
	// 可用优惠劵
	private List<CouponCode> mCouponCodes;
	private boolean iscart = false;
	private long skuId;
	private int num = 0;
	private String mCode = "";
	private String couponType = "";
	private float subPrice;
	private Handler mHandler = new MyHandler(this);


	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_choice_coupon);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle("选择优惠劵");

		mCode = getIntent().getStringExtra("mCode");
		iscart = getIntent().getBooleanExtra("iscart", false);
		skuId = getIntent().getLongExtra("skuId", 0);
		num = getIntent().getIntExtra("num", 0);


		mAdapter = new ChoiceCouponAdapter(this, R.layout.item_coupon_list);
		mChoiceCouponList.setEmptyView(mEmptyRelat);
		mChoiceCouponList.setAdapter(mAdapter);

		getCouponList();

	}


	/** 获取可用优惠劵  */
	private void getCouponList(){
		new Thread(){
			public void run() {
				try {
					mCouponCodes = WebClient.getInstance(ChoiceCouponActivity.this).getCanUseCouponList(iscart, skuId+"", num+"");
				} catch (WebException e) {
					if (WebException.TYPE_SERVER_RETURN_NULL.equals(e.getStatusCode()))
						mCouponCodes = null;
					else
						e.printStackTrace();
				}
				mHandler.sendEmptyMessage(0);
			};
		}.start();

	}

	public void onItemClick(AdapterView<?> parent,  View v, int position, long id){
		if( parent == mChoiceCouponList){
			Intent intent = new Intent();
			
			if( mCode.equals(mAdapter.getData().get(position).getCouponCode())){
				intent.putExtra("mCode", ""); 
				
			}else {
				mCode = mAdapter.getData().get(position).getCouponCode();
				couponType  = mAdapter.getData().get(position).getCoupon().getCouponType().toString();
				
				App.showLog("couponType=="+couponType);
			
				intent.putExtra("mCode", mCode);
				intent.putExtra("couponType", couponType);
				
				if("fullreduction".equals(couponType)){
					subPrice = mAdapter.getData().get(position).getCoupon().getPreferentialAmount();
					intent.putExtra("subPrice", subPrice);
					App.showLog("subPrice=="+subPrice);
				}	
			}
			
			setResult(500, intent);  
			finish();
		}
	}

	@SuppressLint("HandlerLeak") 
	private class MyHandler extends Handler{
		private WeakReference<Activity> mActivity;

		private MyHandler(Activity activity){
			mActivity = new WeakReference<Activity>(activity);
		}

		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if( mActivity.get() != null ){
				switch (msg.what) {
				case 0:
					mAdapter.update(mCouponCodes);
					break;
				default:
					break;
				}

			}
		}

	}

	public class  ChoiceCouponAdapter extends AbsViewHolderAdapter<CouponCode>{
		public ChoiceCouponAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(final int pos, CouponCode itemData) {

			TextView mText1 = getViewFromHolder(R.id.item_coupon_text1);
			TextView mText2 = getViewFromHolder(R.id.item_coupon_text2);
			TextView mText3 = getViewFromHolder(R.id.item_coupon_number);
			TextView mText4 = getViewFromHolder(R.id.item_coupon_time_out);
			ImageView mImg = getViewFromHolder(R.id.coupon_status_img);
			ImageView mImg1 = getViewFromHolder(R.id.coupon_select_img);

			String mCouponType = itemData.getCoupon().getCouponType().toString();
			
			if("fullgive".equals(mCouponType)){
				mText1.setText("赠");
			}else if("fullreduction".equals(mCouponType)){
				mText1.setText("￥"+itemData.getCoupon().getPreferentialAmount() );
			}else if("gift".equals(mCouponType)){
				mText1.setText("礼品");
			}else if("cash".equals(mCouponType)){
				mText1.setText("现金");
			}
			mText2.setText(itemData.getCoupon().getName());
			mText3.setText(itemData.getCouponCode());
			mText4.setText(DateTimeUtil.getDateTime1(itemData.getCoupon().getBeginDate())+"--"+DateTimeUtil.getDateTime1(itemData.getCoupon().getEndDate()));
			mImg.setVisibility(View.GONE);

			if(itemData.getCouponCode().equals(mCode)){
				mImg1.setVisibility(View.VISIBLE);
			}else {
				mImg1.setVisibility(View.GONE);
			}
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeCallbacksAndMessages(null);
	}


}
